/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package EmployeeServiceAPI;

import BUS.MainController;
import Entity.Employee;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.ws.BindingType;

/**
 *
 * @author HOANG ANH
 */
@WebService()
@BindingType(value = "http://java.sun.com/xml/ns/jaxws/2003/05/soap/bindings/HTTP/")
public class EmployeeServer {

    private MainController control;

    public EmployeeServer() {
        control = new MainController();
    }

    private String checkInput(Employee employee) {
        if (employee.getName().length() < 2) {
            return "Employee name too short";
        }
        if (employee.getCountry().length() < 2) {
            return "Employee country too short";
        }

        Calendar birthday = Calendar.getInstance();
        birthday.setTime(employee.getBirthday());
        Calendar today = Calendar.getInstance();
        int age = today.get(Calendar.YEAR) - birthday.get(Calendar.YEAR);
        if (today.get(Calendar.DAY_OF_YEAR) <= birthday.get(Calendar.DAY_OF_YEAR)) {
            age--;
        }
        if (age < 18) {
            return "Employee must 18 or older ( your age : " + age + " )";
        }
        return "pass";
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "register")
    public String register(@WebParam(name = "employee") Employee employee) {

        String check = checkInput(employee);
        if (!check.equals("pass")) {
            return check;
        }

        try {
            control.Register(employee);
        } catch (SQLException ex) {
            String error = ex.getMessage();
            if (error.contains("Violation of PRIMARY KEY constraint")) {
                return "This name already used.";
            } else {
                return error;
            }
        } catch (ClassNotFoundException ex) {
            return ex.getMessage();
        }
        return "OK";
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getEmpList")
    public ArrayList<Employee> getEmpList() {
        try {
            //TODO write your implementation code here:
            return control.getListEmployee();
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "delEmployee")
    public String delEmployee(@WebParam(name = "name") String name) {
        try {
            int output = control.delEmployee(name);
            if (output < 1) {
                return "Employee '" + name + "' not found";
            }
            return "OK";
        } catch (SQLException ex) {
            return ex.getMessage();
        } catch (ClassNotFoundException ex) {
            return ex.getMessage();
        }
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "updateEmployee")
    public String updateEmployee(@WebParam(name = "employee") Employee employee) {
        try {
            String check = checkInput(employee);
            if (!check.equals("pass")) {
                return check;
            }
            int i = control.updateEmployee(employee);

            if (i < 1) {
                return "Employee '" + employee.getName() + "' not found";
            }
            return "OK";
        } catch (SQLException ex) {
            return ex.getMessage();
        } catch (ClassNotFoundException ex) {
            return ex.getMessage();
        }
    }
}
